package io.adminboot.service.impl;

import io.adminboot.domain.vo.PageVO;
import io.adminboot.entity.SysRole;
import io.adminboot.exception.MyException;
import io.adminboot.repository.extend.SysRoleExtMapper;
import io.adminboot.service.SysRoleMenuService;
import io.adminboot.service.SysRoleService;
import io.adminboot.service.SysUserRoleService;
import io.adminboot.service.SysUserService;
import io.adminboot.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * &#x89d2;&#x8272;
 *
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
	
	@Autowired
	private SysRoleExtMapper mapper;
	
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

	@Override
	public PageVO queryPage(Integer pageNum, Integer pageSize, Long createUserId) {
		List<SysRole> list = mapper.selectList((pageNum - 1) * pageSize, pageSize, createUserId);
		int count = mapper.count(createUserId);
		PageVO page = new PageVO(list, count, pageSize, pageNum);
		return page;
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysRole role) {
        role.setCreateTime(new Date());
        mapper.insert(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRole role) {
        mapper.updateByPrimaryKey(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRoleIds(Long[] roleIds) {
        //删除角色
        mapper.batchDelete(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteByRoleIds(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteByRoleIds(roleIds);
    }


    @Override
	public List<Long> queryRoleIdList(Long createUserId) {
		return mapper.selectRoleIdList(createUserId);
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPrems(SysRole role){
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
		if(role.getCreateUserId() == Constant.SUPER_ADMIN){
			return ;
		}
		
		//查询用户所拥有的菜单列表
		List<Long> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());
		
		//判断是否越权
		if(!menuIdList.containsAll(role.getMenuIdList())){
			throw new MyException("新增角色的权限，已超出你的权限范围");
		}
	}

	@Override
	public SysRole selectById(Long roleId) {
		return mapper.selectByPrimaryKey(roleId);
	}

	@Override
	public List<SysRole> selectByCreateUserId(Long createUserId) {
		return mapper.selectByCreateUserId(createUserId);
	}
}
